package 面试;

import java.util.concurrent.TimeUnit;

class Data{
    volatile int name = 0;

    public void addName(){
        this.name = 60;
    }
}
public class volatile可见性 {
    public static void main(String[] args) {
        Data data = new Data();
        new Thread (() -> {
            System.out.println(Thread.currentThread().getName() + "\t come in");
            try {
                TimeUnit.SECONDS.sleep(3);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            data.addName();
            System.out.println(Thread.currentThread().getName() + "\t update name " + "\t " + data.name);
        },"AAA").start();

        while(data.name == 0){}
        System.out.println(Thread.currentThread().getName() + "\t mission is over!" + data.name);
    }
}
